package cn.bby.tools.component;

import cn.bby.tools.utils.ClassChooseUtil;
import cn.bby.tools.utils.MessageUtil;
import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.psi.PsiClass;
import com.intellij.util.ui.JBUI;
import lombok.Getter;
import lombok.Setter;

import javax.swing.*;
import java.awt.*;
import java.util.function.Consumer;

/**
 * @description: ClassChooseComponent
 * @author: bby
 * @date: 2025/11/4 10:05
 * @version: 1.0
 */
public class ClassChooseComponent extends BaseValidationComponent {
    private final Project project;
    private final String title;
    private JTextField selClassField;
    private JButton selectClassButton;
    private JLabel classLabel;
    @Setter
    private int insetVal = 5;
    @Getter
    private PsiClass selClass;
    @Setter
    private Consumer<PsiClass> classConsumer;

    public ClassChooseComponent(Project project, String label) {
        this(project, label, label);
    }

    public ClassChooseComponent(Project project, String label, String title) {
        initializeComponents(label);
        setupLayout();
        if (StrUtil.isEmpty(title)) {
            this.title = MessageUtil.getMessage("message.SelectClass");
        } else {
            this.title = MessageUtil.getMessage(title);
        }
        selectClassButton.addActionListener(e -> {
            PsiClass psiClass = ClassChooseUtil.chooseClass(this.title, project, selClass);
            if (psiClass != null) {
                selClassField.setText(psiClass.getQualifiedName());
                selClass = psiClass;
                classConsumer.accept(selClass);
            }
        });
        this.project = project;
    }

    private void initializeComponents(String label) {
        selClassField = new JTextField(30);
        selClassField.setEditable(false);
        selectClassButton = new JButton(MessageUtil.getMessage("message.SelectClass"));
        classLabel = new JLabel(MessageUtil.getMessageSuffix(label));
    }

    private void setupLayout() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.WEST;

        // 第一列：标签
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = JBUI.insets(insetVal, 0, insetVal, insetVal); // 上、右、下、左间距
        add(classLabel, gbc);

        // 第二列：文本框
        gbc.gridx = 1;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = JBUI.insets(insetVal, 0, insetVal, insetVal); // 上、右、下、左间距
        add(selClassField, gbc);

        // 第三列：选择类按钮
        gbc.gridx = 2;
        gbc.weightx = 0;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = JBUI.insets(insetVal, 0); // 上、右、下、左间距，右侧不需要边距
        add(selectClassButton, gbc);
    }

    public String getSelClassName() {
        return selClassField.getText();
    }

    public void setSelClass(String className) {
        selClass = ClassChooseUtil.findClass(project, className);
        if (selClass != null) {
            selClassField.setText(selClass.getQualifiedName());
        }
    }

    @Override
    public ValidationInfo doValidate() {
        if (StrUtil.isEmpty(selClassField.getText())) {
            return new ValidationInfo(MessageUtil.getMessage("message.SelectClassPlease"), selClassField);
        }
        return null;
    }
}